package com.wzy.config;

import com.wzy.handle.*;
import com.wzy.handle.event.SubscribeHandler;
import com.wzy.handle.event.UnSubscribeHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: wzy
 * @Date: 2022/04/29/15:43
 * @Description: 路由
 */
@Configuration
public class WxJavaConfig {
    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private TextHandler textHandler;

    @Autowired
    private ImageHandler imageHandler;

    @Autowired
    private VideoHandler videoHandler;

    @Autowired
    private VoiceHandler voiceHandler;
    @Autowired
    private SubscribeHandler subscribeHandler;

    @Autowired
    private UnSubscribeHandler unSubscribeHandler;

    @Autowired
    private WxMessageRedisDuplicateChecker wxMessageRedisDuplicateChecker;

    @Bean
    public WxMpMessageRouter messageRouter() {
        // 创建消息路由
        final WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
        // 自定义基于Redis的去重器
        router.setMessageDuplicateChecker(wxMessageRedisDuplicateChecker);
        // 添加文本消息路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT).handler(textHandler).end();
        // 添加图片消息路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.IMAGE).handler(imageHandler).end();
        // 视频消息路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.VIDEO).handler(videoHandler).end();
        // 语音路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.VOICE).handler(voiceHandler).end();

        // 添加关注事件路由器
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE)
                .handler(subscribeHandler).end();
        // 添加取消关注事件路由器
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unSubscribeHandler).end();
        return router;
    }
}
